﻿#include "CBeltSqlOper.h"
#include "CPublicData.h"
#include "CBeltDataBase.h"
CBeltSqlOper::CBeltSqlOper()
{
}

CBeltSqlOper::~CBeltSqlOper()
{
}

QString selectDeviceInfo(QString sId, int & nType, QString & sErr)
{
	QString sGuid = "";
	CDatabaseExecData cData;
	cData.sTableName = "pbelt_device_info";
	cData.sSql = "select * from pbelt_device_info where ID=?";
	cData.vecField.clear();
	cData.vecValue.clear();
	cData.vecField.push_back("ID");
	cData.vecValue.push_back(sId);
	bool bRet = CPublicData::instance().m_pSqlDllOper->exec(cData);
	sErr = cData.sErr;
	QVector<QSqlRecord> vecDataInfo = cData.vecRecord;
	if (vecDataInfo.size() > 0)
	{
		sGuid = vecDataInfo[0].value("GUID").toString().trimmed();
		nType = vecDataInfo[0].value("DEVICE_TYPE").toString().toInt();
	}
	return sGuid;
}


bool selectBeltData(CBeltDataBase * pBeltData)
{
	CDatabaseExecData cData;
	cData.sTableName = "pbelt_tear_point_config";
	cData.sSql = "select * from pbelt_tear_point_config where POINT_GUID=? ";
	cData.addValue("POINT_GUID", pBeltData->m_sGuid);
	bool bRet = CPublicData::instance().m_pSqlDllOper->exec(cData);
	QString sErr = cData.sErr;
	QVector<QSqlRecord> vecDataInfo = cData.vecRecord;
	if (!bRet)
	{
		return false;
	}
	if (vecDataInfo.size() > 0)
	{
		pBeltData->m_pMutex->lock();
		pBeltData->m_sBeltGuid = vecDataInfo[0].value("DEVICE_GUID").toString();
		pBeltData->m_sGuid = vecDataInfo[0].value("POINT_GUID").toString();
		//pBeltData->m_sId = vecDataInfo[0].value("SCENE_GUID").toString();
		pBeltData->m_sBeltName = vecDataInfo[0].value("BELT_NAME").toString().trimmed();
		pBeltData->m_sName = vecDataInfo[0].value("POINT_NAME").toString().trimmed();
		pBeltData->m_sCameraIp = vecDataInfo[0].value("CAMERA_IP").toString().trimmed();
		QString sVlcStr = "rtsp://" + vecDataInfo[0].value("TRANS_SRV_IP").toString().trimmed() + ":8558/" + pBeltData->m_sCameraIp;
		pBeltData->m_sRtspUrl = sVlcStr;
		pBeltData->m_dKaMax = vecDataInfo[0].value("KAMAX").toDouble();
		pBeltData->m_nMaskSum = vecDataInfo[0].value("MASKSUM").toInt();
		pBeltData->m_nLineHight = vecDataInfo[0].value("LINEHIGHT").toInt();
		pBeltData->m_nAlarmType = vecDataInfo[0].value("ALARM_TYPE").toInt();
		pBeltData->m_pMutex->unlock();
	}
	return true;
}

bool selectPoint(QString sGuid, QMap<QString, QString> &mapPoint)
{
	CDatabaseExecData cData;
	cData.sTableName = "pbelt_point_info";
	cData.sSql = "select * from pbelt_point_info where GUID=? ";
	cData.addValue("GUID", sGuid);
	bool bRet = CPublicData::instance().m_pSqlDllOper->exec(cData);
	QString sErr = cData.sErr;
	QVector<QSqlRecord> vecDataInfo = cData.vecRecord;
	if (!bRet)
	{
		return false;
	}
	for (auto i = 0; i < vecDataInfo.size(); i++)
	{
		QString sPointIndex= QString::number(vecDataInfo[i].value("POINT_INDEX").toInt());
		QString sPointAddr = vecDataInfo[i].value("POINT_ADDR").toString();
		mapPoint.insert(sPointIndex, sPointAddr);
	}
	return true;
}
